package 笔试;

import java.util.Arrays;

public class Z字形变换 {
    public String convert(String s, int numRows) {
        if(numRows == 1) {
            return s;
        }
        String[] res = new String[numRows];
        Arrays.fill(res, "");
        char[] arr = s.toCharArray();

        int prod = numRows * 2 - 2;

        for(int i = 0; i < arr.length; i++) {
            int mod = i % prod;
            if(mod < numRows) {
                res[mod] += arr[i];
            } else {
                res[prod-mod] += arr[i];
            }
        }


        StringBuilder sb = new StringBuilder();
        for(String subStr : res) {
            sb.append(subStr);
        }

        return sb.toString();

    }
}
